package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.ruoyi.common.annotation.AutoFill;
import com.ruoyi.common.enums.AutoFillType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BasicSmsConfig;
import com.ruoyi.system.domain.dto.MailRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BasicEmailConfigMapper;
import com.ruoyi.system.domain.BasicEmailConfig;
import com.ruoyi.system.service.IBasicEmailConfigService;

/**
 * 邮箱配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-02
 */
@Service
public class BasicEmailConfigServiceImpl implements IBasicEmailConfigService 
{
    @Autowired
    private BasicEmailConfigMapper basicEmailConfigMapper;

    /**
     * 查询邮箱配置
     * 
     * @param emailConfigId 邮箱配置主键
     * @return 邮箱配置
     */
    @Override
    public BasicEmailConfig selectBasicEmailConfigByEmailConfigId(Long emailConfigId)
    {
        return basicEmailConfigMapper.selectBasicEmailConfigByEmailConfigId(emailConfigId);
    }

    /**
     * 查询邮箱配置列表
     * 
     * @param basicEmailConfig 邮箱配置
     * @return 邮箱配置
     */
    @Override
    public List<BasicEmailConfig> selectBasicEmailConfigList(BasicEmailConfig basicEmailConfig)
    {
        return basicEmailConfigMapper.selectBasicEmailConfigList(basicEmailConfig);
    }

    /**
     * 新增邮箱配置
     * 
     * @param basicEmailConfig 邮箱配置
     * @return 结果
     */
    @AutoFill(operation = AutoFillType.INSERT)
    @Override
    public int insertBasicEmailConfig(BasicEmailConfig basicEmailConfig)
    {
        return basicEmailConfigMapper.insertBasicEmailConfig(basicEmailConfig);
    }

    /**
     * 修改邮箱配置
     * 
     * @param basicEmailConfig 邮箱配置
     * @return 结果
     */
    @AutoFill(operation = AutoFillType.UPDATE)
    @Override
    public int updateBasicEmailConfig(BasicEmailConfig basicEmailConfig)
    {
        return basicEmailConfigMapper.updateBasicEmailConfig(basicEmailConfig);
    }

    /**
     * 批量删除邮箱配置
     * 
     * @param emailConfigIds 需要删除的邮箱配置主键
     * @return 结果
     */
    @Override
    public int deleteBasicEmailConfigByEmailConfigIds(Long[] emailConfigIds)
    {
        return basicEmailConfigMapper.deleteBasicEmailConfigByEmailConfigIds(emailConfigIds);
    }

    /**
     * 删除邮箱配置信息
     * 
     * @param emailConfigId 邮箱配置主键
     * @return 结果
     */
    @Override
    public int deleteBasicEmailConfigByEmailConfigId(Long emailConfigId)
    {
        return basicEmailConfigMapper.deleteBasicEmailConfigByEmailConfigId(emailConfigId);
    }

    //注入邮件工具类
    @Autowired(required = false)
    private JavaMailSender javaMailSender;


    /**
     * 测试连接邮件服务器
     *
     * @param mailRequest
     * @return
     */
    @Override
    public boolean testMailServer(MailRequest mailRequest) {
        Integer emailConfigId = mailRequest.getEmailConfigId();
        String sendTo = mailRequest.getSendTo();
        String subject = mailRequest.getSubject();
        String text = mailRequest.getText();
        String filePath = mailRequest.getFilePath();
        BasicEmailConfig basicEmailConfig = basicEmailConfigMapper.selectBasicEmailConfigByEmailConfigId(Long.valueOf(emailConfigId));
        if(basicEmailConfig == null) throw new BaseException("配置不存在");
        Long supplierId = basicEmailConfig.getSupplierId();
        String emailAccount = basicEmailConfig.getEmailAccount();
        String smtpServer = basicEmailConfig.getSmtpServer();
        Long smtpPort = basicEmailConfig.getSmtpPort();
        String smtpUsername = basicEmailConfig.getSmtpUsername();
        String smtpPassword = basicEmailConfig.getSmtpPassword();
        Integer isSslEnabled = basicEmailConfig.getIsSslEnabled();
        Integer isTlsEnabled = basicEmailConfig.getIsTlsEnabled();
        Integer isActive = basicEmailConfig.getIsActive();

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(smtpServer); // 设置邮件服务器
        mailSender.setPort(smtpPort.intValue());
        mailSender.setUsername(smtpUsername);
        mailSender.setPassword(smtpPassword);

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.starttls.required", true);
        props.put("mail.smtp.ssl.enable", isSslEnabled.equals(1) ? true : false);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        return false;
    }
}
